﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;

namespace YouTubeStormer
{
    public partial class Configuration : Form
    {
        Timer exampleGenerator;

        public Configuration()
        {
            InitializeComponent();
            Icon = Properties.Resources.configureIco;

            statusStrip.Renderer = new ClippingToolStripRenderer();
            exampleGenerator = new Timer { Interval = 4000 };
            exampleGenerator.Tick += Configuration_Tick;
        }
        
        void Configuration_Tick(object sender, EventArgs e)
        {
            GenerateExample();
        }

        void GenerateExample()
        {
            toolStripStatusLabelRealExample.Text = CommentPosting.GenerateComment();
        }

        void LoadSettings()
        {
            richTextBoxTitleP1.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP1));
            richTextBoxTitleP2.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP2));
            richTextBoxTitleP3.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP3));
            richTextBoxTitleP4.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP4));
            richTextBoxTitleP5.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP5));
            richTextBoxTitleP6.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.CommentsP6));
            richTextBoxLinks.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.Links));
            richTextBoxVideoIDs.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.VideoIDs));
            richTextBoxAccounts.Text = String.Join(Environment.NewLine, StringCollectionToArray(Properties.Settings.Default.Accounts));
            numericUpDownCommentsPerAccount.Value = Properties.Settings.Default.CommentsPerAccount;
            numericUpDownVotesPerAccount.Value = Properties.Settings.Default.VotesPerAccount;
            numericUpDownTimerInterval.Value = Properties.Settings.Default.TimerInterval;
            numericUpDownVotesPerVideo.Value = Properties.Settings.Default.VotesPerVideo;
            checkBoxShowDebugInformation.Checked = Properties.Settings.Default.ShowDebugInfo;
        
        }

        public static string[] StripPasswordsFromAccounts()
        {
            var accounts = StringCollectionToArray(Properties.Settings.Default.Accounts);
            for (int i = 0; i < accounts.Length; i++)
            {
                accounts[i] = accounts[i].Split(':')[0];
            }
            return accounts;
        }

        public static string[] StringCollectionToArray(StringCollection collection)
        {
            Random ran = new Random();
            var array = new string[collection.Count];
            collection.CopyTo(array, 0);
            return array.OrderBy(x => ran.Next()).ToArray();
        }

        void SaveSettings()
        {

            Properties.Settings.Default.CommentsP1.Clear();
            Properties.Settings.Default.CommentsP2.Clear();
            Properties.Settings.Default.CommentsP3.Clear();
            Properties.Settings.Default.CommentsP4.Clear();
            Properties.Settings.Default.CommentsP5.Clear();
            Properties.Settings.Default.CommentsP6.Clear();
            Properties.Settings.Default.Links.Clear();
            Properties.Settings.Default.VideoIDs.Clear();
            Properties.Settings.Default.Accounts.Clear();

            Properties.Settings.Default.CommentsP1.AddRange(richTextBoxTitleP1.Lines);
            Properties.Settings.Default.CommentsP2.AddRange(richTextBoxTitleP2.Lines);
            Properties.Settings.Default.CommentsP3.AddRange(richTextBoxTitleP3.Lines);
            Properties.Settings.Default.CommentsP4.AddRange(richTextBoxTitleP4.Lines);
            Properties.Settings.Default.CommentsP5.AddRange(richTextBoxTitleP5.Lines);
            Properties.Settings.Default.CommentsP6.AddRange(richTextBoxTitleP6.Lines);
            Properties.Settings.Default.Links.AddRange(richTextBoxLinks.Lines);
            Properties.Settings.Default.VideoIDs.AddRange(richTextBoxVideoIDs.Lines);
            Properties.Settings.Default.Accounts.AddRange(richTextBoxAccounts.Lines);

            Properties.Settings.Default.CommentsPerAccount = (int)numericUpDownCommentsPerAccount.Value;
            Properties.Settings.Default.VotesPerAccount = (int)numericUpDownVotesPerAccount.Value;
            Properties.Settings.Default.TimerInterval = (int)numericUpDownTimerInterval.Value;
            Properties.Settings.Default.VotesPerVideo = (int)numericUpDownVotesPerVideo.Value;
            Properties.Settings.Default.ShowDebugInfo = checkBoxShowDebugInformation.Checked;

            Properties.Settings.Default.Save();

        }

        private void Configuration_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        void CalculatePossibleComments()
        {
            int possible =
                richTextBoxTitleP1.Lines.Length *
                richTextBoxTitleP2.Lines.Length *
                richTextBoxTitleP3.Lines.Length *
                richTextBoxTitleP4.Lines.Length *
                richTextBoxTitleP5.Lines.Length *
                richTextBoxTitleP6.Lines.Length *
                richTextBoxLinks.Lines.Length;

            groupBoxCommentParts.Text = "Comment Parts - " + possible + " possible combos";
        }

        private void richTextBoxTitleP_KeyDown(object sender, KeyEventArgs e)
        {
            CalculatePossibleComments();    
        }

        private void Configuration_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                LoadSettings();
                CalculatePossibleComments();
                GenerateExample();
                exampleGenerator.Start();
            }
            else exampleGenerator.Stop();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveSettings();
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private class ClippingToolStripRenderer : ToolStripSystemRenderer
        {
            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                ToolStripStatusLabel label = e.Item as ToolStripStatusLabel;
                if (label != null) TextRenderer.DrawText(e.Graphics, label.Text, label.Font, e.TextRectangle, label.ForeColor, TextFormatFlags.EndEllipsis);
                else base.OnRenderItemText(e);
            }
        }

        private void statusStrip1_Resize(object sender, EventArgs e)
        {
            toolStripStatusLabelRealExample.Width = (sender as Control).Width - 20;
        }

    }
}
